package cn.ht.jywrwater.module.ht.controller.admin.waterqualitydata;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;

import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;

import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ht.jywrwater.module.ht.controller.admin.waterqualitydata.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterqualitydata.WaterQualityDataDO;
import cn.ht.jywrwater.module.ht.convert.waterqualitydata.WaterQualityDataConvert;
import cn.ht.jywrwater.module.ht.service.waterqualitydata.WaterQualityDataService;

@Tag(name = "管理后台 - 水质数据")
@RestController
@RequestMapping("/ht/water-quality-data")
@Validated
public class WaterQualityDataController {

    @Resource
    private WaterQualityDataService waterQualityDataService;

    @PostMapping("/create")
    @Operation(summary = "创建水质数据")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-data:create')")
    public CommonResult<Long> createWaterQualityData(@Valid @RequestBody WaterQualityDataCreateReqVO createReqVO) {
        return success(waterQualityDataService.createWaterQualityData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新水质数据")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-data:update')")
    public CommonResult<Boolean> updateWaterQualityData(@Valid @RequestBody WaterQualityDataUpdateReqVO updateReqVO) {
        waterQualityDataService.updateWaterQualityData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除水质数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:water-quality-data:delete')")
    public CommonResult<Boolean> deleteWaterQualityData(@RequestParam("id") Long id) {
        waterQualityDataService.deleteWaterQualityData(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得水质数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-data:query')")
    public CommonResult<WaterQualityDataRespVO> getWaterQualityData(@RequestParam("id") Long id) {
        WaterQualityDataDO waterQualityData = waterQualityDataService.getWaterQualityData(id);
        return success(WaterQualityDataConvert.INSTANCE.convert(waterQualityData));
    }

    @GetMapping("/list")
    @Operation(summary = "获得水质数据列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-data:query')")
    public CommonResult<List<WaterQualityDataRespVO>> getWaterQualityDataList(@RequestParam("ids") Collection<Long> ids) {
        List<WaterQualityDataDO> list = waterQualityDataService.getWaterQualityDataList(ids);
        return success(WaterQualityDataConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得水质数据分页")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-data:query')")
    public CommonResult<PageResult<WaterQualityDataRespVO>> getWaterQualityDataPage(@Valid WaterQualityDataPageReqVO pageVO) {
        PageResult<WaterQualityDataDO> pageResult = waterQualityDataService.getWaterQualityDataPage(pageVO);
        return success(WaterQualityDataConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出水质数据 Excel")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-data:export')")
    @OperateLog(type = EXPORT)
    public void exportWaterQualityDataExcel(@Valid WaterQualityDataExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WaterQualityDataDO> list = waterQualityDataService.getWaterQualityDataList(exportReqVO);
        // 导出 Excel
        List<WaterQualityDataExcelVO> datas = WaterQualityDataConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "水质数据.xls", "数据", WaterQualityDataExcelVO.class, datas);
    }

}
